package tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class FileTurn {
	public static byte[] file_to_byte(String filename) {
		// debug
		System.out.println(String.format(
				"filename = %s turn to byte(FileTurn.java)", filename));
		byte[] b = null;
		try {
			File file = new File(filename);
			b = new byte[(int) file.length()];
			FileInputStream is = new FileInputStream(file);
			is.read(b);

			// debug
			System.out.println(String
					.format("after  read to byte[](FileTurn.java)"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}

	public static Map<String, Integer> wordcount_file_to_byte(String filename) {
		// debug
		System.out.println(String.format(
				"filename = %s turn to byte(FileTurn.java)", filename));
		String word;
		Integer count;
		Map<String, Integer> wordcountMap = new HashMap<String, Integer>();
		try {
			Scanner cin = new Scanner(new FileReader(filename));

			while (cin.hasNext()) {
				word = cin.next();
				if ((count = wordcountMap.get(word)) == null) {
					count = 0;
				}
				count += 1;
				wordcountMap.put(word, count);
			}

			System.out.println(String
					.format("after  read to wordcountMap(FileTurn.java)"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return wordcountMap;
	}
}
